package com.hzjh.base.http

import com.hzjh.base.http.gson.GsonConverterFactory
import com.hzjh.base.http.interceptor.LoggingInterceptor
import com.hzjh.base.http.interceptor.TokenInterceptor
import com.hzjh.base.http.interceptor.UrlInterceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import java.util.concurrent.TimeUnit

/**
 * 设置retrofit使用okhttp请求
 * 以及对okhttp的设置
 */
class HttpClient {
    companion object {

        private const val TIME_OUT = 30

        private val okHttpClient by lazy {
            return@lazy OkHttpClient().newBuilder()
                .addInterceptor(UrlInterceptor())
                .addInterceptor(TokenInterceptor())
                .addInterceptor(LoggingInterceptor.init())
                .connectTimeout(TIME_OUT.toLong(), TimeUnit.SECONDS)
                .readTimeout(TIME_OUT.toLong(), TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build()
        }

        internal fun createRetrofit(baseUrl: String): Retrofit {
            return Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }

    }
}